package top.yokey.shopnc.activity.mine;

import android.support.v7.widget.AppCompatEditText;
import android.support.v7.widget.AppCompatSpinner;
import android.support.v7.widget.AppCompatTextView;
import android.support.v7.widget.LinearLayoutCompat;
import android.support.v7.widget.Toolbar;
import android.text.TextUtils;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;

import top.yokey.shopnc.R;
import top.yokey.shopnc.base.BaseApplication;
import top.yokey.base.base.BaseHttpListener;
import top.yokey.base.base.BaseSnackBar;
import top.yokey.base.base.BaseToast;
import top.yokey.shopnc.base.SwipeActivity;
import top.yokey.base.bean.BaseBean;
import top.yokey.shopnc.model.MemberInvoiceModel;
import top.yokey.base.util.JsonUtil;

import org.json.JSONArray;
import org.json.JSONException;
import org.xutils.view.annotation.ContentView;
import org.xutils.view.annotation.ViewInject;

import java.util.Vector;

@ContentView(R.layout.activity_mine_invoice_add)
public class InvoiceAddActivity extends SwipeActivity {

    @ViewInject(R.id.mainLinearLayout)
    private LinearLayoutCompat mainLinearLayout;
    @ViewInject(R.id.mainToolbar)
    private Toolbar mainToolbar;

    @ViewInject(R.id.personTextView)
    private AppCompatTextView personTextView;
    @ViewInject(R.id.companyTextView)
    private AppCompatTextView companyTextView;

    @ViewInject(R.id.headerLinearLayout)
    private LinearLayoutCompat headerLinearLayout;
    @ViewInject(R.id.headerEditText)
    private AppCompatEditText headerEditText;
    @ViewInject(R.id.titleSpinner)
    private AppCompatSpinner titleSpinner;
    @ViewInject(R.id.saveTextView)
    private AppCompatTextView saveTextView;

    private String selectString;
    private String titleString;
    private String contentString;
    private Vector<String> mainVector;

    @Override
    public void initData() {

        setToolbar(mainToolbar, "添加发票");

        mainVector = new Vector<>();
        selectString = "person";
        titleString = "个人";
        contentString = "";

        getInvoiceContent();

    }

    @Override
    public void initEven() {

        personTextView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                selectString = "person";
                headerLinearLayout.setVisibility(View.GONE);
                personTextView.setBackgroundResource(R.drawable.selector_border_primary);
                companyTextView.setBackgroundResource(R.drawable.selector_border_grey_add);
            }
        });

        companyTextView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                selectString = "company";
                headerLinearLayout.setVisibility(View.VISIBLE);
                personTextView.setBackgroundResource(R.drawable.selector_border_grey_add);
                companyTextView.setBackgroundResource(R.drawable.selector_border_primary);
            }
        });

        titleSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
                contentString = mainVector.get(i);
            }

            @Override
            public void onNothingSelected(AdapterView<?> adapterView) {

            }
        });

        saveTextView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                addInvoice();
            }
        });

    }

    //自定义方法

    private void addInvoice() {

        if (selectString.equals("person")) {
            titleString = "个人";
        } else {
            titleString = headerEditText.getText().toString();
        }

        if (TextUtils.isEmpty(selectString) || TextUtils.isEmpty(titleString) || TextUtils.isEmpty(contentString)) {
            BaseSnackBar.get().show(mainLinearLayout, "请输入完整内容！");
            return;
        }

        saveTextView.setEnabled(false);
        saveTextView.setText("添加中...");

        MemberInvoiceModel.get().invoiceAdd(selectString, titleString, contentString, new BaseHttpListener() {
            @Override
            public void onSuccess(BaseBean baseBean) {
                saveTextView.setEnabled(true);
                saveTextView.setText("保存发票");
                BaseToast.get().showSuccess();
                BaseApplication.get().finish(getActivity());
            }

            @Override
            public void onFailure(String reason) {
                saveTextView.setEnabled(true);
                saveTextView.setText("保存发票");
            }
        });

    }

    private void getInvoiceContent() {

        BaseSnackBar.get().showHandler(mainLinearLayout);

        MemberInvoiceModel.get().invoiceContentList(new BaseHttpListener() {
            @Override
            public void onSuccess(BaseBean baseBean) {
                try {
                    String data = JsonUtil.getDatasString(baseBean.getDatas(), "invoice_content_list");
                    JSONArray jsonArray = new JSONArray(data);
                    for (int i = 0; i < jsonArray.length(); i++) {
                        mainVector.add(jsonArray.getString(i));
                    }
                    ArrayAdapter<String> arrayAdapter = new ArrayAdapter<>(getActivity(), R.layout.simple_spinner_item, mainVector);
                    arrayAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
                    titleSpinner.setAdapter(arrayAdapter);
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onFailure(String reason) {
                BaseSnackBar.get().show(mainLinearLayout, reason);
            }
        });

    }

}
